<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 请求参数验证
 */
use think\Validate;
use config\Config;

class Validator extends Validate
{
    public function __construct($method, $route)
    {

    }

    protected function platform(...$args): bool|string
    {
        $value = $args[0];
        if(isset(Config::PLATFORM_MAP[$value])){
            return true;
        }else{
            return 'platform not found!';
        }
    }

    protected function timestamp($value, $rule, $data, $field, $title): bool|string
    {
        $t = time();
        if($value > $t - 10 and $value <= $t){
            return true;
        }else{
            return "request expired!";
        }
    }

    protected function smscode($value, $rule, $data, $field, $title): bool|string
    {
        if(preg_match('/^\d{4}$/', $value)) {
            return true;
        }else{
            return 'code format error!';
        }
    }

    protected function device($value, $rule, $data, $field, $title): bool|string
    {
        if(strlen($value) <= 32){
            return true;
        }else{
            return 'device format error!';
        }
    }

    protected function os($value, $rule, $data, $field, $title): bool|string
    {
        if(strlen($value) <= 32){
            return true;
        }else{
            return 'os format error!';
        }
    }

    protected function version($value, $rule, $data, $field, $title): bool|string
    {
        if(preg_match('/^\d{1}\.\d{1,2}\.\d{1,2}$/', $value)) {
            return true;
        }else{
            return 'version format error!';
        }
    }



}